package com.itheima.controller.sms;


import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.sms.SmsHomeAdvertise;
import com.itheima.service.sms.SmsHomeAdvertiseService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告模块
 */
@RestController
@RequestMapping("home/advertise")
public class SmsHomeAdvertiseController {

    @DubboReference
    private SmsHomeAdvertiseService homeAdvertiseService;

    /**
     * 添加广告
     *
     * @param homeAdvertise
     * @return
     */
    @PostMapping("create")
    public R creat(@RequestBody SmsHomeAdvertise homeAdvertise) {

        homeAdvertiseService.save(homeAdvertise);
        return R.success("添加成功");
    }

    /**
     * 删除广告
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        homeAdvertiseService.removeByIds(ids);

        return R.success("删除成功");
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @param endTime
     * @return
     */
    @GetMapping("list")
    public R list( @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize")Integer pageSize,
                              @RequestParam(value = "name",required = false)String name,
                              @RequestParam(value = "type",required = false)Integer type,
                              @RequestParam(value = "endTime",required = false)String endTime)
    {
       PageInfo page = homeAdvertiseService.getList(pageNum,pageSize,name,type,endTime);
        return R.success(page);
    }

    /**
     * 修改上下线状态
     *
     *
     */
    @PostMapping("update/status/{id}")
    public R updateStatus(@PathVariable("id") Integer id, @RequestParam Integer status) {
        homeAdvertiseService.updateStatus(id,status);
        return R.success("修改状态成功");
    }


    /**
     * 修改广告
     * @param homeAdvertise
     * @return
     */
    @PostMapping("update/{id}")
    public R update( @PathVariable Long id,@RequestBody SmsHomeAdvertise homeAdvertise){
        homeAdvertise.setId(id);
        homeAdvertiseService.updateById(homeAdvertise);
        return R.success("修改成功");
    }
    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getAdvertiseId(@PathVariable Integer id) {
        SmsHomeAdvertise homeAdvertise = homeAdvertiseService.getById(id);
        return R.success(homeAdvertise);
    }
}
